﻿using System.IO;
using NUnit.Framework;
using Rhino.Mocks;
using SimpleServiceBus.Bus;
using SimpleServiceBus.Bus.MessageManagement.Serialization;
using SimpleServiceBus.Bus.Pipeline;
using SimpleServiceBus.Bus.Pipeline.PipelineComponents;
using SimpleServiceBus.Tests.Stubs;

namespace SimpleServiceBus.Tests.Bus.Pipeline.PipelineComponents
{
    [TestFixture]
    public class MessageBodySerializationComponentTestFixture
    {
        [Test]
        public void Test_Componet_Deserializes_For_Receieve_Pipeline()
        {
            var comp = new MessageBodySerializationComponent();
            var pipeline = MockRepository.GenerateStub<IMessagePipeline>();

            var env = new MessageEnvelope {Body = "Hello"};
            env.Payload = new byte[0];

            var context = new ServiceLocatorOnlyMessageContext();

            var formatter = MockRepository.GenerateStub<IMessageSerializer>();
            context.RegisterService<IMessageSerializer>(formatter);

            pipeline.Stub(action => action.CurrentContext).Return(context);
            pipeline.Stub(action => action.PipelineDirection).Return(PipelineDirection.Receive);

            formatter.Stub(action => action.Deserialize(null)).IgnoreArguments().Return("I was deserialized!");

            comp.Pipeline = pipeline;
            comp.ProcessMessage(env);

            formatter.AssertWasCalled(action => action.Deserialize(null),a=>a.IgnoreArguments());

            Assert.AreEqual(env.Body, "I was deserialized!");
        }

        [Test]
        public void Test_Componet_Serialize_For_Send_Pipeline()
        {
            var comp = new MessageBodySerializationComponent();
            var pipeline = MockRepository.GenerateStub<IMessagePipeline>();

            var env = new MessageEnvelope {Body = "Hello"};

            var context = new ServiceLocatorOnlyMessageContext();

            var formatter = MockRepository.GenerateStub<IMessageSerializer>();
            context.RegisterService<IMessageSerializer>(formatter);

            pipeline.Stub(action => action.CurrentContext).Return(context);
            pipeline.Stub(action => action.PipelineDirection).Return(PipelineDirection.Send);

            //formatter.Stub(action => action.Serialize(env.Body, null));

            comp.Pipeline = pipeline;
            comp.ProcessMessage(env);

            formatter.AssertWasCalled(action => action.Serialize(env.Body, null),a=>a.IgnoreArguments());
        }
    }
}